//
//  PasswordInputView.m
//  lhpc
//
//  Created by 李英杰 on 2017/9/2.
//  Copyright © 2017年 lhpc. All rights reserved.
//

#import "PasswordInputView.h"

#define Itemwidth ([UIScreen mainScreen].bounds.size.width - 34) / 6
#define Itemheigt (([UIScreen mainScreen].bounds.size.width - 34) / 6) * 0.9

#define LineWidth 0.8

#define blackcolor [UIColor blackColor]
#define clearcolor [UIColor clearColor]
#define bordercColor [UIColor lightGrayColor]

@interface PasswordInputView ()

@property (nonatomic, strong) UIView * inputBackView;

@property (nonatomic, strong) UILabel * firstLineLabel;
@property (nonatomic, strong) UILabel * secondLineLabel;
@property (nonatomic, strong) UILabel * thirdLineLabel;
@property (nonatomic, strong) UILabel * fourthLineLabel;
@property (nonatomic, strong) UILabel * fifthLineLabel;

@property (nonatomic, strong) UIImageView * firstLabel;
@property (nonatomic, strong) UIImageView * secondLabel;
@property (nonatomic, strong) UIImageView * thirdLabel;
@property (nonatomic, strong) UIImageView * fourthLabel;
@property (nonatomic, strong) UIImageView * fifthLabel;
@property (nonatomic, strong) UIImageView * sixthLabel;


@property (nonatomic, strong) UITextField * textfield;


@property (nonatomic, copy) NSString * passwordStr;


@end

@implementation PasswordInputView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupChildViews];
    }
    return self;
}

-(void)setupChildViews{
    
    [self addSubview:self.inputBackView];
    [self.inputBackView addSubview:self.firstLineLabel];
    [self.inputBackView addSubview:self.secondLineLabel];
    [self.inputBackView addSubview:self.thirdLineLabel];
    [self.inputBackView addSubview:self.fourthLineLabel];
    [self.inputBackView addSubview:self.fifthLineLabel];
    
    [self.inputBackView addSubview:self.firstLabel];
    [self.inputBackView addSubview:self.secondLabel];
    [self.inputBackView addSubview:self.thirdLabel];
    [self.inputBackView addSubview:self.fourthLabel];
    [self.inputBackView addSubview:self.fifthLabel];
    [self.inputBackView addSubview:self.sixthLabel];
    
    
    [self.inputBackView addSubview:self.textfield];
    
    [self.inputBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.centerX.centerY.equalTo(self);
        make.height.mas_equalTo(Itemheigt + LineWidth*2);
        make.width.mas_equalTo(Itemwidth*6 + LineWidth*7);
        
    }];
    
    NSArray * lineArray = @[self.firstLineLabel,self.secondLineLabel,self.thirdLineLabel,self.fourthLineLabel,self.fifthLineLabel];
    
    
    [lineArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:Itemwidth leadSpacing:Itemwidth+0.1 tailSpacing:Itemwidth+0.1];

    
    [lineArray mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(self.inputBackView);
        make.width.mas_equalTo(LineWidth);
        
    }];
    

    [self.textfield becomeFirstResponder];
    
    [self updateConstraints];
}


-(void)textFieldDidChangeText:(id)sender{
    
    UITextField * textField = (UITextField *)sender;
    
    
    
    if (textField.text.length == 0) {
        self.firstLabel.backgroundColor = clearcolor;
        self.secondLabel.backgroundColor = clearcolor;
        self.thirdLabel.backgroundColor = clearcolor;
        self.fourthLabel.backgroundColor = clearcolor;
        self.fifthLabel.backgroundColor = clearcolor;
        self.sixthLabel.backgroundColor = clearcolor;
        
        
    }else if (textField.text.length == 1){
        self.firstLabel.backgroundColor = blackcolor;
        self.secondLabel.backgroundColor = clearcolor;
        self.thirdLabel.backgroundColor = clearcolor;
        self.fourthLabel.backgroundColor = clearcolor;
        self.fifthLabel.backgroundColor = clearcolor;
        self.sixthLabel.backgroundColor = clearcolor;
        
    }else if (textField.text.length == 2){
        self.firstLabel.backgroundColor = blackcolor;
        self.secondLabel.backgroundColor = blackcolor;
        self.thirdLabel.backgroundColor = clearcolor;
        self.fourthLabel.backgroundColor = clearcolor;
        self.fifthLabel.backgroundColor = clearcolor;
        self.sixthLabel.backgroundColor = clearcolor;
        
    }else if (textField.text.length == 3){
        self.firstLabel.backgroundColor = blackcolor;
        self.secondLabel.backgroundColor = blackcolor;
        self.thirdLabel.backgroundColor = blackcolor;
        self.fourthLabel.backgroundColor = clearcolor;
        self.fifthLabel.backgroundColor = clearcolor;
        self.sixthLabel.backgroundColor = clearcolor;
        
    }else if (textField.text.length == 4){
        self.firstLabel.backgroundColor = blackcolor;
        self.secondLabel.backgroundColor = blackcolor;
        self.thirdLabel.backgroundColor = blackcolor;
        self.fourthLabel.backgroundColor = blackcolor;
        self.fifthLabel.backgroundColor = clearcolor;
        self.sixthLabel.backgroundColor = clearcolor;
        
    }else if (textField.text.length == 5){
        self.firstLabel.backgroundColor = blackcolor;
        self.secondLabel.backgroundColor = blackcolor;
        self.thirdLabel.backgroundColor = blackcolor;
        self.fourthLabel.backgroundColor = blackcolor;
        self.fifthLabel.backgroundColor = blackcolor;
        self.sixthLabel.backgroundColor = clearcolor;
        
    }else if (textField.text.length == 6){
        self.firstLabel.backgroundColor = blackcolor;
        self.secondLabel.backgroundColor = blackcolor;
        self.thirdLabel.backgroundColor = blackcolor;
        self.fourthLabel.backgroundColor = blackcolor;
        self.fifthLabel.backgroundColor = blackcolor;
        self.sixthLabel.backgroundColor = blackcolor;
        
        
        self.passwordStr = textField.text;
        
        if ([self.delegate respondsToSelector:@selector(passwordHasInPut:)]) {
            [self.delegate passwordHasInPut:self.passwordStr];
        }
        
    }else{
        
        textField.text = self.passwordStr;
        
    }
}

-(void)updateConstraints{
    [super updateConstraints];
    
    [self.firstLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(self.inputBackView);
        make.centerX.equalTo(self.inputBackView.mas_left).mas_offset(Itemwidth/2);
        make.size.mas_equalTo(CGSizeMake(Itemwidth * 0.3, Itemwidth * 0.3));
        
    }];
    
    [self.secondLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.inputBackView);
        make.centerX.equalTo(self.inputBackView.mas_left).mas_offset(Itemwidth/2 + (Itemwidth + LineWidth));
        make.size.equalTo(self.firstLabel);
        
    }];
    
    [self.thirdLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.inputBackView);
        make.centerX.equalTo(self.inputBackView.mas_left).mas_offset(Itemwidth/2 + (Itemwidth + LineWidth)*2);
        make.size.equalTo(self.firstLabel);
        
    }];
    
    [self.fourthLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.inputBackView);
        make.centerX.equalTo(self.inputBackView.mas_left).mas_offset(Itemwidth/2 + (Itemwidth + LineWidth)*3);
        make.size.equalTo(self.firstLabel);
        
    }];
    
    [self.fifthLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.inputBackView);
        make.centerX.equalTo(self.inputBackView.mas_left).mas_offset(Itemwidth/2 + (Itemwidth + LineWidth)*4);
        make.size.equalTo(self.firstLabel);
        
    }];
    
    [self.sixthLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.inputBackView);
        make.centerX.equalTo(self.inputBackView.mas_left).mas_offset(Itemwidth/2 + (Itemwidth + LineWidth)*5);
        make.size.equalTo(self.firstLabel);
        
    }];
    
    [self.textfield mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self.inputBackView);
        
    }];
    
}


-(UIView *)inputBackView{
    
    if (!_inputBackView) {
        _inputBackView = [[UIView alloc]init];
        _inputBackView.layer.borderWidth = LineWidth;
        _inputBackView.layer.borderColor = bordercColor.CGColor;
        _inputBackView.layer.cornerRadius = 3;
        
    }
    return _inputBackView;
    
}

-(UILabel *)firstLineLabel{
    if (!_firstLineLabel) {
        _firstLineLabel = [[UILabel alloc]init];
        _firstLineLabel.backgroundColor = bordercColor;
        
    }
    return _firstLineLabel;
}

-(UILabel *)secondLineLabel{
    if (!_secondLineLabel) {
        _secondLineLabel = [[UILabel alloc]init];
        _secondLineLabel.backgroundColor = bordercColor;
        
    }
    return _secondLineLabel;
}

-(UILabel *)thirdLineLabel{
    if (!_thirdLineLabel) {
        _thirdLineLabel = [[UILabel alloc]init];
        _thirdLineLabel.backgroundColor = bordercColor;
        
    }
    return _thirdLineLabel;
}

-(UILabel *)fourthLineLabel{
    if (!_fourthLineLabel) {
        _fourthLineLabel = [[UILabel alloc]init];
        _fourthLineLabel.backgroundColor = bordercColor;
        
    }
    return _fourthLineLabel;
}

-(UILabel *)fifthLineLabel{
    if (!_fifthLineLabel) {
        _fifthLineLabel = [[UILabel alloc]init];
        _fifthLineLabel.backgroundColor = bordercColor;
        
    }
    return _fifthLineLabel;
}

-(UIImageView *)firstLabel{
    if (!_firstLabel) {
        _firstLabel = [[UIImageView alloc]init];
        _firstLabel.layer.cornerRadius = Itemwidth * 0.3 * 0.5;
        _firstLabel.backgroundColor = clearcolor;
        
    }
    return _firstLabel;
}

-(UIImageView *)secondLabel{
    if (!_secondLabel) {
        _secondLabel = [[UIImageView alloc]init];
        _secondLabel.layer.cornerRadius = Itemwidth * 0.3 * 0.5;
        _secondLabel.backgroundColor = clearcolor;
        
    }
    return _secondLabel;
}

-(UIImageView *)thirdLabel{
    if (!_thirdLabel) {
        _thirdLabel = [[UIImageView alloc]init];
        _thirdLabel.layer.cornerRadius = Itemwidth * 0.3 * 0.5;
        _thirdLabel.backgroundColor = clearcolor;
        
    }
    return _thirdLabel;
}

-(UIImageView *)fourthLabel{
    if (!_fourthLabel) {
        _fourthLabel = [[UIImageView alloc]init];
        _fourthLabel.layer.cornerRadius = Itemwidth * 0.3 * 0.5;
        _fourthLabel.backgroundColor = clearcolor;
        
    }
    return _fourthLabel;
}

-(UIImageView *)fifthLabel{
    if (!_fifthLabel) {
        _fifthLabel = [[UIImageView alloc]init];
        _fifthLabel.layer.cornerRadius = Itemwidth * 0.3 * 0.5;
        _fifthLabel.backgroundColor = clearcolor;
        
    }
    return _fifthLabel;
}

-(UIImageView *)sixthLabel{
    if (!_sixthLabel) {
        _sixthLabel = [[UIImageView alloc]init];
        _sixthLabel.layer.cornerRadius = Itemwidth * 0.3 * 0.5;
        _sixthLabel.backgroundColor = clearcolor;
        
    }
    return _sixthLabel;
}

-(UITextField *)textfield{
    if (!_textfield) {
        _textfield = [[UITextField alloc]init];
        _textfield.backgroundColor = [UIColor clearColor];
        _textfield.keyboardType = UIKeyboardTypeNumberPad;
        _textfield.textColor = [UIColor clearColor];
        _textfield.borderStyle = UITextBorderStyleNone;
        _textfield.tintColor = [UIColor clearColor];
        [_textfield addTarget:self action:@selector(textFieldDidChangeText:) forControlEvents:UIControlEventEditingChanged];
        
    }
    return _textfield;
}


@end
